package com.github.jdsjlzx.recyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import vid.recyclertest.R;

/**
 * Created by Lzx on 16/11/30 0030.
 */
public class MRecyclerViewAdapter extends LRecyclerViewAdapter {

    public static final String TAG = "MRecyclerViewAdapter";

    public final static int TYPE_EMPTY = 0x12340002;
    public final static int TYPE_LOADING = 0x12340003;
    public final static int TYPE_ERROR = 0x12340004;
    public final static int TYPE_FOOTER = 0x12340005;
    public enum State{Loading, Empty, Error, Normal}
    State state;

    public View loadingView;
    public View emptyView;
    public View errorView;

    private boolean showLoadingFooter = false;
    public View footerView;

    public MRecyclerViewAdapter(final LRecyclerView recyclerView, RecyclerView.Adapter innerAdapter) {
        super(innerAdapter);
        state = State.Empty;
        emptyView = LayoutInflater.from(recyclerView.getContext()).inflate(R.layout.layout_empty, recyclerView, false);
        loadingView = LayoutInflater.from(recyclerView.getContext()).inflate(R.layout.layout_loading, recyclerView, false);
        errorView = LayoutInflater.from(recyclerView.getContext()).inflate(R.layout.layout_error, recyclerView, false);
        footerView = LayoutInflater.from(recyclerView.getContext()).inflate(R.layout.layout_footer, recyclerView, false);

        View.OnClickListener refreshClick = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showLoading();
                recyclerView.setRefreshing(true);
            }
        };
        setOnEmptyClickListener(refreshClick);
        setOnErrorClickListener(refreshClick);

        setEmptyView(emptyView);
        setLoadingView(loadingView);
        setErrorView(errorView);
        setFooterView(footerView);
    }

    public void setLoadingView(View loadingView) {
        this.loadingView = loadingView;
    }

    public void setEmptyView(View emptyView) {
        this.emptyView = emptyView;
    }

    public void setErrorView(View errorView) {
        this.errorView = errorView;
    }

    public void setFooterView(View footerView) {
        this.footerView = footerView;
        showLoadingFooter();
    }

    public void showLoadingFooter() {
        this.showLoadingFooter = true;
    }

    public void showError() {
        state = State.Error;
        notifyDataSetChanged();
    }

    public void showEmpty() {
        state = State.Empty;
        notifyDataSetChanged();
    }

    public void showLoading() {
        state = State.Loading;
        notifyDataSetChanged();
    }

    public void showContent() {
        state = State.Normal;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        int count = super.getItemCount();
        if (count > 1) {
            state = State.Normal;
            return showLoadingFooter ? count+1 : count;
        }
        switch (state) {
            case Empty:
            case Loading:
            case Error:
                return 1;
            case Normal:
                if (count <= 1) {
                    //refreshing header
                    state = State.Empty;
                    return 1;
                }
        }
        //never go to here....
        return count;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (state) {
            case Empty:
                return new StateViewHolder(emptyView);
            case Loading:
                return new StateViewHolder(loadingView);
            case Error:
                return new StateViewHolder(errorView);
        }
        if (viewType == TYPE_FOOTER) {
            return new StateViewHolder(footerView);
        }
        return super.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        if (!isSpecialType(type)) {
            super.onBindViewHolder(holder, position);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List<Object> payloads) {
        int type = getItemViewType(position);
        if (!isSpecialType(type)) {
            super.onBindViewHolder(holder, position, payloads);
        }
    }

    private boolean isSpecialType(int type) {
        return type == TYPE_FOOTER
                || type == TYPE_EMPTY
                || type == TYPE_ERROR
                || type == TYPE_LOADING;
    }

    @Override
    public int getItemViewType(int position) {
        switch (state) {
            case Empty:
                return TYPE_EMPTY;
            case Loading:
                return TYPE_LOADING;
            case Error:
                return TYPE_ERROR;
        }
        if (showLoadingFooter && position == getItemCount()-1) {
            return TYPE_FOOTER;
        }
        return super.getItemViewType(position);
    }

    public void setOnErrorClickListener(View.OnClickListener clickListener) {
        errorView.setOnClickListener(clickListener);
    }

    public void setOnEmptyClickListener(View.OnClickListener clickListener) {
        emptyView.setOnClickListener(clickListener);
    }

    public class StateViewHolder extends RecyclerView.ViewHolder {

        public StateViewHolder(View itemView) {
            super(itemView);
            if (itemView.getParent() != null) {
                ((ViewGroup)itemView.getParent()).removeView(itemView);
            }
        }

    }
}
